//
//  MainBannerItemCell.m
//  WIFI
//
//  Created by macbook on 2022/8/8.
//

#import "MainBannerItemCell.h"
#import <StoreKit/StoreKit.h>
#import "UIButton+WebCache.h"
#import "CustomWebViewController.h"
#import "MineSettingViewController.h"
#import "ProductDetailViewController.h"

@interface MainBannerItemCell ()<SKStoreProductViewControllerDelegate>

@property (nonatomic, strong) UIButton *backImg;

@end

@implementation MainBannerItemCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self createUI];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.backgroundColor = [UIColor clearColor];
        [self createUI];
    }
    return self;
}

- (void)createUI {
    _backImg = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth-40, ceil((KScreenWidth-40)*0.29253))];
    _backImg.backgroundColor = ColorHex(@"#FFFFFF");
    [_backImg addTarget:self action:@selector(bannerClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_backImg];
}

- (void)bannerClick:(UIButton *)sender {
    NSString *recent = [NSString stringWithFormat:@"%@",[_imgUrl safeObjectForKey:@"recent"]];
    
    if ([recent rangeOfString:@"http://"].location != NSNotFound ||
        [recent rangeOfString:@"https://"].location != NSNotFound) {
        //webView
        CustomWebViewController *webVC = [[CustomWebViewController alloc] init];
        webVC.linkUrl = recent;
        [[AppTools getCurrentVC].navigationController pushViewController:webVC animated:YES];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/thelike"].location != NSNotFound) {
        //设置
        MineSettingViewController *settingVC = [[MineSettingViewController alloc] init];
        settingVC.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:settingVC animated:YES];
        return;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/andlove"].location != NSNotFound) {
        //首页
        [AppTools getCurrentVC].tabBarController.selectedIndex = 0;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/standard"].location != NSNotFound) {
        //登录
        [AppTools toLogin];
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/classes"].location != NSNotFound) {
        //订单
        [AppTools getCurrentVC].tabBarController.selectedIndex = 1;
        return;
    }
    if ([recent rangeOfString:@"pe://so.gui.de/islove"].location != NSNotFound) {
        //产品详情
        NSArray *array = [recent componentsSeparatedByString:@"="];
        NSString *theId = [NSString stringWithFormat:@"%@",[array safeObjectAtIndex:1]];
        
        ProductDetailViewController *vc = [[ProductDetailViewController alloc] init];
        vc.productId = theId;
        vc.hidesBottomBarWhenPushed = YES;
        [[AppTools getCurrentVC].navigationController pushViewController:vc animated:YES];
        return;
    }
}

- (void)setImgUrl:(NSDictionary *)imgUrl {
    _imgUrl = imgUrl;
    
    NSString *traceback = [NSString stringWithFormat:@"%@",[imgUrl safeObjectForKey:@"traceback"]];
    
    [_backImg sd_setImageWithURL:[NSURL URLWithString:traceback] forState:0];
}

@end
